﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Drawing.Drawing2D;
using GameEditor.Common.Controls;
using GameEditor.Common;
using System.Windows.Forms;


namespace TaskSystemClient
{
    class PriorityCellPainter : ICellPainter
    {
        public void PaintCell(Graphics dc, Rectangle cellRect, TreeListNode node, TreeListColumn column, TextFormatting format)
        {
            CatalogTreeNodeData nodeData = node.Tag as CatalogTreeNodeData;
            if (nodeData.TaskData == null)
                return;

            int index = Array.IndexOf(SPM.TaskPriority.Priorities, nodeData.TaskData.Task.Priority.ToLower());
            Color c = Color.White;
            if (index != -1)
                c = TaskPriorityEditorDelegate.Colors[index];

            Rectangle r = GameEditor.Common.Utilities.AdjustRectangle(cellRect, format.Padding); ;// cellRect;

            //r.X = column.CalculatedRect.X;
            //r.Width = column.CalculatedRect.Width;

            
            HSB color1 = new GameEditor.Common.HSB(c.GetHue(), c.GetSaturation(), c.GetBrightness());
            HSB color2 = color1;
            color1.Brightness += 0.1f;
            //color1.Hue += 10;
            Color c1 = color1.ToRGB();
            color2.Brightness -= 0.1f;
            //color2.Hue -= 10;
            Color c2 = color2.ToRGB();
            Point p1 = Point.Empty;
            Point p2 = Point.Empty;



            p1 = new Point(r.Left, r.Top);
            p2 = new Point(r.Right, r.Bottom);

            using (LinearGradientBrush brush = new LinearGradientBrush(p1, p2, c1, c2))
            {
                dc.FillRectangle(brush, r);
            }
            string text = nodeData.TaskData.Task.Priority;
            TextFormatFlags flags = TextFormatFlags.EndEllipsis | format.GetFormattingFlags();

            cellRect.Y+=2;
            cellRect.X+=2;
            TextRenderer.DrawText(dc, text, column.Font, cellRect, Color.Black, flags);
            cellRect.Y--;
            cellRect.X--;
            TextRenderer.DrawText(dc, text, column.Font, cellRect, Color.White, flags);


        }
    }
    class ProgressCellPainter : ICellPainter, IDisposable
    {
        public void PaintCell(Graphics dc, Rectangle cellRect, TreeListNode node, TreeListColumn column, TextFormatting format)
        {
            NodeCell cell = node[column.Index];
            MileStoneEditData data = cell.Node.Tag as MileStoneEditData;

            float pc = 0.01f * data.Progress;
            //绘制进度条
            Rectangle progressRect = new Rectangle(cellRect.X, cellRect.Y, (int)(cellRect.Width * pc), cellRect.Height);
            if (progressRect.Width <= 0)
                progressRect.Width = 1;
            using (LinearGradientBrush brush = new LinearGradientBrush(cellRect, Color.SkyBlue, Color.RoyalBlue, 0.0f))
            {
                dc.FillRectangle(brush, progressRect);
            }
            //绘制文字 
            string text = data.Progress + "%";
            Rectangle textRect = cellRect;
            textRect.Y += 2;
            textRect.X += 1;
            Font font = column.Font;

            dc.DrawString(text, font, Brushes.White, textRect, textFormat);
            textRect.X--;
            textRect.Y--;
            dc.DrawString(text, font, Brushes.Black, textRect, textFormat);  
            //绘制边框
            dc.DrawRectangle(Pens.Black, cellRect);
        }
        StringFormat textFormat = new StringFormat();

        public ProgressCellPainter()
        {
            textFormat.Alignment = StringAlignment.Center;
            textFormat.LineAlignment = StringAlignment.Center;
        }
        public void Dispose()
        {
            textFormat.Dispose();
        }
 
    }
}
